﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class SceneLogic
{
    SceneCamera _camera;
    HumanCtrl _humanCtrl;

    TimerMgr.Timer timerLoop;

    // 背景根节点
    GameObject _bgRoot;
    // 地图块根节点
    GameObject _mapRoot;
    // 起始点
    GameObject _startPoint;

    // 被挤死的距离(跟摄像机size有关)
    private const float DeadDistance = 6f;
    // 摔死
    private const float DeadFallDownDis = -3.7f;
    // 移动此距离后，提速
    private const float speedUpDis = 10;
    // 最多加速倍数
    private const float maxSpeedUpMul = 4.3f;

    Transform _palyerTransform;
    // 奔跑距离
    private float runnedDis { get { return _palyerTransform.position.x; } }
    private int runnedDisInt { get { return Mathf.FloorToInt(runnedDis); } }

    private CountDownDlg _startCDDlg; // 开具倒计时

    // 地图管理(动态加载)
    private MapMgr _mapMgr;

    public void Init()
    {
        _startPoint = ResMgr.Instance.GetGameObject("SceneObj/StartPoint");
        var obj = ResMgr.Instance.GetGameObject("SceneObj/Main Camera");
        _camera = obj.AddComponent<SceneCamera>();
        Vector3 sp = GameObject.FindWithTag("StartPoint").transform.position;

        //_mainPlayer = loadMainPlayer(sp);
        //_mainPlayer.Init(_camera, gameOver);

        _humanCtrl = loadHumanCtrl(sp);
        _humanCtrl.Init(_camera, gameOver);
        _palyerTransform = _humanCtrl.transform;

        _bgRoot = ResMgr.Instance.GetGameObject("SceneObj/Background");
        _mapRoot = ResMgr.Instance.GetGameObject("SceneObj/SceneBlock");

        _mapMgr = new MapMgr();
        _mapMgr.Init(_camera, _mapRoot, _bgRoot);

        // 开局倒计时
        _startCDDlg = DialogMgr.Instance.OpenDialog<CountDownDlg>();

        timerLoop = TimerMgr.Instance.CreateTimer(0, -1, loop);
        loop();

        var timerCount = TimerMgr.Instance.CreateTimer(1, 3, loopStartCountdown);
        loopStartCountdown();
        timerCount.Start();

    }

    private void startRun()
    {
        _humanCtrl.Begin();
        timerLoop.Start();
    }

    private int countdownNum = 1;
    private void loopStartCountdown()
    {
        _startCDDlg.Num = 4 - countdownNum;
        if (countdownNum > 3)
        {
            Debug.Log("go");
            _startCDDlg.SetActive(false);
            startRun();
        }
        ++countdownNum;
    }

    internal void Reset()
    {
        timerLoop.Stop();
    }

    private void loop()
    {
        checkAndSpeedUp();
        showRunDistance();

        _mapMgr.CheckAndLoad();

        checkGameOver();
    }

    // 加速
    private void checkAndSpeedUp()
    {
        var dis = runnedDis;
        float mul = 1 + Mathf.Min(maxSpeedUpMul, Mathf.Max(0f, (int)(dis / speedUpDis)) / 10f); // 每移动speedUpDis米，提速10%。最多提到maxSpeedUpMul倍速度
        _humanCtrl.SpeedUp(_humanCtrl.DefaultMoveSpeed * mul);
    }

    // 显示奔跑距离
    private void showRunDistance()
    {
        FightUIMgr.Instance.SetDistance(runnedDisInt);
    }

    // 检测游戏结束
    private void checkGameOver()
    {
        // 被挤死
        var dis = _camera.transform.position.x - _palyerTransform.position.x;
        if (dis > DeadDistance)
        {
            gameOver();
            return;
        }
        // 掉下去
        if (_palyerTransform.position.y < DeadFallDownDis)
        {
            gameOver();
            return;
        }
    }

    GameOverDlg _gameOverDlg;
    private void gameOver()
    {
        Debug.Log("gameOver");
        GameMgr.Instance.Pause = true;
        Time.timeScale = 0;
        _humanCtrl.Release();

        _gameOverDlg = DialogMgr.Instance.OpenDialog<GameOverDlg>();
        _gameOverDlg.SetBtnCallback(restart, exit);
        _gameOverDlg.SetText(9999.ToString(), runnedDisInt.ToString());
    }

    private void restart()
    {
        GameMgr.Instance.Pause = false;
        Time.timeScale = 1;
        DialogMgr.Instance.Close(_gameOverDlg);
        SceneMgr.Instance.LoadScene("Scenes/Running");
    }

    private void exit()
    {
        Application.Quit();
    }

    private HumanCtrl loadHumanCtrl(Vector3 sp)
    {
        var pObj = ResMgr.Instance.GetGameObject("Role/Nan/Nan", sp);
        HumanCtrl Player = pObj.AddComponent<HumanCtrl>();

        return Player;
    }
}
